package com.example.sls.dao.logistics;

import com.example.sls.pojo.logistics.UserManageTable;
import com.example.sls.pojo.logistics.UserManagement;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 用户管理表(UserManagement)表数据库访问层
 *
 * @author makejava
 * @since 2024-06-24 08:24:25
 */
public interface UserManagementDao {
  /**
   * 通过ID查询单条数据
   *
   * @param id 主键
   * @return 实例对象
   */
  @Select("select * from user_management where id=#{id}")
  UserManagement queryById(Integer id);
  /**
   * 查询指定行数据
   *
   * @param userManagement 查询条件
   * @param pageSize       分页对象
   * @param offset         偏移量，起始处
   * @return 对象列表
   */
  @Select("select * from user_management where 1=1" +
          " and (id=#{userManagement.id} or (isnull(#{userManagement.id}) or #{userManagement.id}=0))" +
          " and (user_id=#{userManagement.userId} or (isnull(#{userManagement.userId}) or #{userManagement.userId}=0))" +
          " and (blacklist=#{userManagement.blacklist} or (isnull(#{userManagement.blacklist}) or #{userManagement.blacklist}=0))" +
          " and (blacklist_days=#{userManagement.blacklistDays} or (isnull(#{userManagement.blacklistDays}) or #{userManagement.blacklistDays}=0))" +
          " limit #{pageSize} offset #{offset}")
  List<UserManagement> queryAllByLimit(UserManagement userManagement, Integer pageSize, Integer offset);
  /**
   * 连表查询
   *
   * @return 对象列表
   */
  @Select("select um.id,username,blacklist,blacklist_days from user_management as um right join users u on u.id = um.user_id")
  List<UserManageTable>getTableData();
  /**
   * 统计总行数
   *
   * @return 总行数
   */
  @Select("select count(*) from user_management")
  long count();
  /**
   * 新增数据
   *
   * @param userManagement 实例对象
   * @return 影响行数
   */
  @Insert("insert into user_management (id,user_id,blacklist,blacklist_days) " +
          "values(" +
          "if(#{id}!=0 and !isnull(#{id}),#{id},null)," +
          "if(!isnull(#{userId}),#{userId},null)," +
          "if(!isnull(#{blacklist}),#{blacklist},null)," +
          "if(!isnull(#{blacklistDays}),#{blacklistDays},null)" +
          ")")
  int insert(UserManagement userManagement);
  /**
   * 通过id修改数据
   * id 主键
   *
   * @param userManagement 实例对象
   * @return 影响行数
   */
  @Update("update user_management set " +
          "user_id=if(isnull(#{userId}),user_id,#{userId})," +
          "blacklist=if(isnull(#{blacklist}),blacklist,#{blacklist})," +
          "blacklist_days=if(isnull(#{blacklistDays}),blacklist_days,#{blacklistDays})" +
          " where id=#{id}")
  int update(UserManagement userManagement);
  /**
   * 通过主键删除数据
   *
   * @param id 主键
   * @return 影响行数
   */
  @Delete("delete from user_management where id=#{id}")
  int deleteById(Integer id);
}

